package com.sping.cloud.zuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/7/18.
 */
public class AccessFilter extends ZuulFilter {

    private static Logger logger = LoggerFactory.getLogger(AccessFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {

        RequestContext cxt = RequestContext.getCurrentContext();
        HttpServletRequest request  = cxt.getRequest();

        logger.info("send:{}, request to :{}", request.getMethod(), request.getRequestURL());

        Object access = request.getParameter("name");
        if(access == null){
            logger.info("name is empty");
            cxt.setSendZuulResponse(false);
            cxt.setResponseStatusCode(401);
            return null;
        }

        logger.info("name is ok");
        return null;
    }
}
